import datetime

from mongoengine import EmailField, BooleanField, DateTimeField, StringField, URLField, ListField
from flask_mongoengine import Document
from flask_login import UserMixin


class User(Document, UserMixin):
    email = EmailField(unique=True)
    password = StringField(required=True, min_length=6)
    active = BooleanField(default=True)
    isAdmin = BooleanField(default=False)
    timestamp = DateTimeField(default=datetime.datetime.now())

    def get_id(self):
        return self.pk

    def is_active(self):
        return self.active


class EntryDocument(Document):
    """
    基类。保存了实体的创建和修改时间。
    """
    meta = {'abstract': True}

    date_modified = DateTimeField(default=datetime.datetime.now)
    date_created = DateTimeField()

    def save(self, *args, **kwargs):
        if not self.data_created:
            self.data_created = datetime.datetime.now()
        self.date_modified = datetime.datetime.now()
        return super(EntryDocument, self).save(*args, **kwargs)


class Game(EntryDocument):
    """
    游戏实体类

        >>> g = Game(title='Mount & Blade')
        >>> g.save()
        <Game: Game object>
        >>> g.id
        ObjectId('546b4f075d5377617d56940d')
        >>> g.title_translation
        []
        >>> g.title_translation.append('骑马于砍杀')
        >>> g.title_translation
        ['骑马于砍杀']
        >>> g.title_translation.append('骑马砍人')
        >>> g.title_translation
        ['骑马于砍杀', '骑马砍人']
        >>> g.data_created
        datetime.datetime(2014, 11, 18, 21, 52, 7, 31625)
        >>> g.date_modified
        datetime.datetime(2014, 11, 18, 21, 52, 7, 31710)
        >>> g.save()
        <Game: Game object>
        >>> g.date_modified
        datetime.datetime(2014, 11, 18, 21, 59, 17, 364551)
        >>> g.data_created
        datetime.datetime(2014, 11, 18, 21, 52, 7, 31625)

    """
    title = StringField(max_length=200, required=True, unique=True)
    title_translation = ListField(StringField(max_length=200))
    developer = StringField(max_length=200)
    developer_site = URLField(max_length=200)
    intro = StringField(max_length=2000)

